(function(window) {
    // ------------------------------------------
    // let temp = document.querySelector('.main').querySelector('input[type="number"]');
    // let temp2 = document.querySelector('.main').querySelector('#btnAdd');
    // temp2.addEventListener('click', function() {
    //     console.log(temp.value);
    // });
    // ----------------------------------------------
    const PAGE = 5; //一页展示的行数
    class Donate {
        constructor() {
                const COMPANY = ['德玛西亚', '诺克萨斯', '艾欧尼亚', '暗影岛', '弗雷尔卓德', '比尔吉沃特', '恕瑞玛', '祖安'];
                // 公司名字常量
                this.COMPANY = COMPANY;
                // 捐款对象的名字
                this.OBJ = ['name', 'company', 'money', 'date'];
                // 表示目前状态是正在修改的标记
                this.currentModi = 0;
                // 记录当前显示的页面总数
                this.pageAll = 0;
                // 记录当前的页面序号
                this.currentPage = 0;
                // 记录当前显示的列表名
                this.nowShow = 'donate';
                //记录当前显示的具体公司
                this.search = 'all';
                this.localData = new localData();
                // 捐款数据对象
                this.data = {
                        // 具体捐款表
                        'donate': [],
                        // 捐款目录
                        'company': {}
                    }
                    // [
                    //     {
                    //         name,
                    //         company,
                    //         money,
                    //         date,
                    //         id
                    //     }
                    // ]

                // {
                //     'company':[listindex]
                // }
                // 页面渲染出来时将select框填入内容
                window.addEventListener('DOMContentLoaded', function() {
                    this.loadCompany(document.querySelector('#donateCompany'), COMPANY);
                    this.loadCompany(document.querySelector('#company'), COMPANY);
                }.bind(this));
            }
            // 加载select框的方法
        loadCompany(selCom, arr, selected) {
                // 遍历数据
                for (let val of arr) {
                    let temp = document.createElement('option');
                    temp.value = val;
                    if (val === selected) temp.selected = true;
                    temp.innerHTML = val;
                    selCom.appendChild(temp);
                }
            }
            // 退回表格显示的方法（即修改时点取消）
        backShow(table) {
            // 把处于修改状态的表格挨个还原
            for (let i = 1; i < table.children[this.currentModi].children.length - 1; i++) {
                table.children[this.currentModi].children[i].innerHTML = `${this.data['donate'][this.currentModi][this.OBJ[i-1]]}`;
            }
        }
        makeContent() { //生成目录company
                // 先清空目录
                for (let i = 0; i < this.COMPANY.length; i++) {
                    this.data['company'][this.COMPANY[i]] = [];
                }
                // 然后依据现在的数据生成目录
                for (let i = 0; i < this.data['donate'].length; i++) {
                    this.data['company'][this.data['donate'][i].company].push(i);
                }
                // 保存数据
                this.saveData();
            }
            // 删除数据
        delData(arr, index) {
            // 删除完完整列表后再生成目录
            this.data['donate'].splice(index, 1);
            this.makeContent();
        }
        showData(table, key, company) {
            // 展示数据的方法
            // 传入key时，就执行对应的展示代码
            //没有传入key时，就默认处于当前展示列表
            key = key || this.nowShow;
            // 然后将当前展示的指针指向当前需要展示的列表
            this.nowShow = key;
            company = company || this.search;
            this.loadData(key);
            removeTb(table);
            let iter = 0;
            let x = 5;
            switch (key) {
                case 'donate':
                    this.pageAll = Math.floor((this.data[key].length - 1) / PAGE);
                    if (this.pageAll < 0) this.pageAll = 0;
                    if (this.currentPage > this.pageAll) this.currentPage--;
                    iter = this.currentPage * PAGE;
                    x = iter + 5 >= this.data[key].length ? this.data[key].length - iter : 5;
                    for (let i = iter; i < iter + x; i++) {
                        let tr = document.createElement('tr');
                        let td = document.createElement('td');
                        td.innerHTML = i + 1;
                        tr.appendChild(td);
                        for (let val in this.data[key][i]) {
                            td = document.createElement('td');
                            td.innerHTML = this.data[key][i][val];
                            tr.appendChild(td);
                        }
                        td = document.createElement('td');
                        td.className = 'oper';
                        td.dataset.index = `${i}`;
                        td.innerHTML = `<a href="javascript:;" class="del">删除</a><a href="javascript:;"  class="modi">修改</a>`;
                        tr.appendChild(td);
                        table.appendChild(tr);
                    }
                    break;
                case 'company':
                    this.pageAll = Math.floor((this.data[key][company].length - 1) / PAGE);
                    if (this.pageAll < 0) this.pageAll = 0;
                    if (this.currentPage > this.pageAll) this.currentPage--;
                    iter = this.currentPage * PAGE;
                    x = iter + 5 >= this.data[key][company].length ? this.data[key][company].length - iter : 5;
                    for (let i = iter; i < iter + x; i++) {
                        let tr = document.createElement('tr');
                        let td = document.createElement('td');
                        td.innerHTML = i + 1;
                        tr.appendChild(td);
                        for (let val in this.data['donate'][this.data[key][company][i]]) {
                            td = document.createElement('td');
                            td.innerHTML = this.data['donate'][this.data[key][company][i]][val];
                            tr.appendChild(td);
                        }
                        td = document.createElement('td');
                        td.className = 'oper';
                        td.dataset.index = `${this.data[key][company][i]}`;
                        td.innerHTML = `<a href="javascript:;" class="del">删除</a><a href="javascript:;"  class="modi">修改</a>`;
                        tr.appendChild(td);
                        table.appendChild(tr);
                    }
                    break;
            }
            let pspan = document.querySelector('#pageSpan');
            pspan.innerHTML = `${this.currentPage+1}/${this.pageAll+1}`;
            if (this.currentPage === this.pageAll) {
                document.querySelector('#btnPageDown').disabled = true;
            } else {
                document.querySelector('#btnPageDown').disabled = false;
            }
            if (this.currentPage === 0) {
                document.querySelector('#btnPageUp').disabled = true;
            } else {
                document.querySelector('#btnPageUp').disabled = false;
            }
        }
        modiData(index, arr) { //index表示该行在数据中的索引，arr是该行的dom对象数组
            for (let i = 1; i < 5; i++) {
                this.data['donate'][index][this.OBJ[i - 1]] = arr[i].innerHTML;
            }
            this.makeContent();
        }
        saveData() {
            this.localData.saveData('donate', this.data['donate']);
            this.localData.saveData('company', this.data['company']);
        }
        loadData(key) {
            let temp = this.localData.getData(key);
            if (temp) {
                this.data[key] = temp;
            }
        }
        addDonater(name, company, money, date) {
            this.loadData('donate');
            this.loadData('company');
            //存储结构
            this.data['donate'].unshift({
                name: name,
                company: company,
                money: money,
                date: date
            })
            this.makeContent();
        }
    }

    function removeTb(table) {
        while (table.children.length) {
            table.removeChild(table.children[0]);
        }
    }
    window.Donate = Donate;
}(window))